package com.example.springai.controller;

import com.example.springai.commonConfig.RedisChatMemory;
import com.example.springai.domain.MessageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

import static org.springframework.ai.chat.memory.ChatMemory.CONVERSATION_ID;

@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
public class ChatController {

    private final ChatClient  chatClient;
    private final RedisChatMemory redisChatMemory;
    @RequestMapping(value = "/chat",produces = "text/html;charset=utf-8")
    public Flux<String> chat(String message) {
        String chatId = "chatId123321";
        return chatClient.prompt()
                .user(message)
                .advisors(advisorSpec -> advisorSpec.param(CONVERSATION_ID, chatId))
                .stream()
                .content();
    }

    @GetMapping("/history/{chatId}")
    public List<MessageVO> history(@PathVariable("chatId") String chatId) {
        List<Message> messages = redisChatMemory.get(chatId);
        return messages.stream().map(MessageVO::new).toList();
    }

}
